package com.fht.controller.food;



import com.fht.entity.FoodCategory;

import com.fht.entity.MerchantsInformation;
import com.fht.enums.ManagermentSystemEnum;
import com.fht.from.FoodCategoryFrom;
import com.fht.service.food.FoodCategoryService;
import com.fht.service.food.MerchantsInformationService;

import com.fht.util.ResultVOUtil;
import com.fht.vo.LayUITableVO;

import com.fht.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/food/category")
public class FoodCategoryController {

    @Autowired
    public FoodCategoryService foodCategoryService;
    @Autowired
    public MerchantsInformationService merchantsInformationService;


    //菜品类别展示
    @GetMapping("/index")
    public String index(Model model){

        List<MerchantsInformation> merchantsInformationList = merchantsInformationService.findAllIdAndName();
        model.addAttribute("merchantsInformationList",merchantsInformationList);

        return "food/foodCategory/index";

    }
    //获取菜品类别信息
    @GetMapping("/list")
    @ResponseBody
    public LayUITableVO<List<FoodCategory>> list (@RequestParam(defaultValue = "1") Integer page,
                                                           @RequestParam(defaultValue = "10") Integer limit,
                                                           FoodCategoryFrom from){
        List<FoodCategory> parents = foodCategoryService.findParents(from);

        LayUITableVO< List<FoodCategory>> tableVo = new LayUITableVO();
        tableVo.setData(parents);
        return tableVo;
    }

    //查看菜品类别信息
    @GetMapping("/show")
    public String show(String id,Model model){

        Map<String, Object> map = foodCategoryService.findById(id);
        model.addAttribute("foodCategoryMap",map);


        return "food/foodCategory/show";

    }


    //修改菜品类别信息1（查询指定id菜品分类详情）
    @GetMapping("/edit-ui")
    public String editUI(String id,Model model){

        Map<String, Object> map = foodCategoryService.findById(id);
        FoodCategory parentFoodCategory = (FoodCategory) map.get("parentFoodCategory");
        List<FoodCategory> childFoodCategoryList = (List<FoodCategory>) map.get("childFoodCategoryList");
        model.addAttribute("parentFoodCategory",parentFoodCategory);
        model.addAttribute("childFoodCategory",childFoodCategoryList.get(0));
        model.addAttribute("childFoodCategoryList",childFoodCategoryList);
        return "food/foodCategory/edit";

    }

    //修改菜品类别信息2（查询指定id菜品分类详情）
    @GetMapping("/edit-list")
    @ResponseBody
    public ResultVO editList(String id){


        FoodCategory currentFoodcategory =foodCategoryService.selectById(id);
        currentFoodcategory.setStatus(currentFoodcategory.getStatus().equals("1")?"已启用":"未启用");
        return ResultVOUtil.ok(currentFoodcategory);

    }


    //执行修改
    @PostMapping("/edit")
    @ResponseBody
    public ResultVO edit(@Valid FoodCategory foodCategory, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("【添加APP信息】 接收参数有误！msg={}",msg);
            return ResultVOUtil.error(ManagermentSystemEnum.PARAM_ERROR.getCode(),msg);
        }
        foodCategoryService.update(foodCategory);

        return ResultVOUtil.ok();
    }

    //执行删除
    @PostMapping("/del")
    @ResponseBody
    public ResultVO del(String id){
        foodCategoryService.delelteById(id);
        return ResultVOUtil.ok();
    }

    //跳转至添加页面
    @GetMapping("/add-ui")
    public String addUI(String merchantsId,Model model){

        MerchantsInformation merchantsInformation = merchantsInformationService.selectById(merchantsId);
        model.addAttribute("merchantsInformation",merchantsInformation);
        List<FoodCategory> foodCategoryList = foodCategoryService.selectBymerchantsId(merchantsId);
        model.addAttribute("foodCategoryList",foodCategoryList);
        return "food/foodCategory/add";
    }

    //添加一级菜品类别
    @PostMapping("/addOne")
    @ResponseBody
    public ResultVO addOne(String merchantsId,String newCategoryName){
        if ("".equals(newCategoryName) || null == newCategoryName){
            log.info("【添加一级菜品类别】 接收参数有误！msg={}","参数异常");
            return ResultVOUtil.error(ManagermentSystemEnum.PARAM_ERROR);
        }
        FoodCategory foodCategory = foodCategoryService.addParent(merchantsId,newCategoryName);

        return ResultVOUtil.ok(foodCategory);
    }

    //执行修改
    @PostMapping("/addTwo")
    @ResponseBody
    public ResultVO addTwo(@Valid FoodCategory foodCategory, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("【添加APP信息】 接收参数有误！msg={}",msg);
            return ResultVOUtil.error(ManagermentSystemEnum.PARAM_ERROR.getCode(),msg);
        }
        foodCategoryService.addChild(foodCategory);

        return ResultVOUtil.ok();
    }

}
